package com.yonyou.pmclouds.milestone.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.ref.itf.RefCodeConst;
import com.yonyou.pmclouds.basecom.ref.itf.ReferenceChecker;
import com.yonyou.pmclouds.milestone.entity.MilestoneRefVO;
import com.yonyou.pmclouds.milestone.entity.MilestoneVO;
import com.yonyou.pmclouds.milestone.mapper.MilestoneMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author th
 * @date 2019/11/6 15:22
 */
@Service(interfaceClass = IMilestoneQuery.class, version = ApplicationConsts.APPLICATION_VERSION)
public class MilestoneQueryImpl implements IMilestoneQuery {
    @Autowired
    private MilestoneMapper mapper;
    @Autowired
    private ReferenceChecker referenceChecker;

    @Override
    public MilestoneVO[] queryByTenant(String pkTenant, Byte enableStatus, String keyword) throws BusinessException {
        MilestoneVO[] milestoneVOS = mapper.queryByTenant(pkTenant, enableStatus, keyword);
        return milestoneVOS;
    }

    @Override
    public Boolean referenceCheck(String pk) throws BusinessException {
        return referenceChecker.checkReferenced(RefCodeConst.MILESTONE, new String[]{pk});
    }

    @Override
    public MilestoneRefVO[] queryRefByTenant(String pkTenant, String keyword) throws BusinessException {
        MilestoneRefVO[] milestoneVOs = mapper.queryRefVOByTenant(pkTenant, keyword);
        return milestoneVOs;
    }

    @Override
    public Map<String, String> queryNameById(String[] ids) throws BusinessException {
        if (ArrayUtils.isEmpty(ids))
            return new HashMap<>(0);

        MilestoneRefVO[] milestoneRefVOS = mapper.queryNameByIds(ids);
        if (ArrayUtils.isEmpty(milestoneRefVOS))
            return new HashMap<>(0);

        Map<String, String> result = new HashMap<>();
        for (MilestoneRefVO refVO : milestoneRefVOS) {
            result.put(refVO.getPkMilestone(), refVO.getName());
        }

        return result;
    }

    @Override
    public Map<String, MilestoneVO> queryByCodes(String[] codes, String pkTenant) throws BusinessException {
        if (ArrayUtils.isEmpty(codes))
            return new HashMap<>(0);

        List<MilestoneVO> milestoneVOList = mapper.queryByCodes(codes, pkTenant);
        if (CollectionUtils.isEmpty(milestoneVOList))
            return new HashMap<>(0);

        Map<String, MilestoneVO> result = new HashMap<>();
        for (MilestoneVO vo : milestoneVOList) {
            result.put(vo.getCode(), vo);
        }

        return result;
    }

}
